Object.defineProperty(exports, "__esModule", {
    value: !0
}), exports.default = void 0;

var _util = _interopRequireDefault(require("../public/utils/util")), _promise = _interopRequireDefault(require("../public/utils/promise")), _api = _interopRequireDefault(require("./api"));

function _interopRequireDefault(e) {
    return e && e.__esModule ? e : {
        default: e
    };
}

var app = getApp(), _default = {
    prePay: function() {
        var o = this, e = 0 < arguments.length && void 0 !== arguments[0] ? arguments[0] : {};
        return new _promise.default(function(a, i) {
            var u = e.data, n = e.complete;
            app.getTokens(function(e) {
                var t = e.accesstoken, r = e.openid;
                _api.default.swiftpass.prepay({
                    data: Object.assign({
                        accesstoken: t,
                        openid: r
                    }, u),
                    complete: n
                }).then(function(e) {
                    return o.payment(e.data, a, i);
                }).catch(function(e) {
                    i && i(e), _util.default.qAlert(e.message);
                });
            });
        });
    },
    payment: function(e, t, r) {
        var a = this, i = e.orderid, u = e.jsparams;
        u.success = function() {
            return t({
                orderid: i
            });
        }, u.fail = r, u.complete = function(e) {
            /fail/.test(e.errMsg) && a.cancelOrder(i);
        }, wx.requestPayment(u);
    },
    cancelOrder: function(e) {
        _api.default.swiftpass.cancelorder({
            data: {
                orderid: e
            }
        });
    }
};

exports.default = _default;